package com.qifan.energy.widget.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.qifan.energy.mvp.callback.IEventOptionCallback;
import com.qifan.energy.R;

public class ShareBottomDialog extends DialogFragment {

    private IEventOptionCallback callback = new IEventOptionCallback() {

        @Override
        public void onLeftConfirm() {

        }

        @Override
        public void onRightConfirm() {

        }
    };

    /* 对外提供 */
    public void setEventCallback(IEventOptionCallback callback) {
        this.callback = callback;
    }

    /* 显示 */
    public ShareBottomDialog showNow2(@Nullable FragmentManager manager, @Nullable String tag) {
        showNow(manager, tag);
        return this;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.fragment_dialog_bottom_share, null);
        Dialog dialog = new Dialog(getActivity(), R.style.DialogFullScreen);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(true);

        //Do something
        findView(view);

        // 设置宽度为屏宽、位置靠近屏幕底部
        Window window = dialog.getWindow();
        window.setBackgroundDrawableResource(R.color.transparent);
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(wlp);
        return dialog;
    }

    /* 设置View */
    private void findView(View view) {
        ImageView closeIv = view.findViewById(R.id.close_iv);
        TextView leftTv = view.findViewById(R.id.left_tv);
        TextView rightTv = view.findViewById(R.id.right_tv);
        closeIv.setOnClickListener(v -> {
            dismiss();
        });
        leftTv.setOnClickListener(v -> {
            dismiss();
            if (callback != null) callback.onLeftConfirm();
        });
        rightTv.setOnClickListener(v -> {
            dismiss();
            if (callback != null) callback.onRightConfirm();
        });
    }
}
